package frame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import MyBatis.MyBatislianjian;
import constant.Constant;
import frame.Customer.CustomerPanel;
import frame.Employee.EmployeePanel;
import frame.Good.GoodPanel;
import frame.PayDetail.PayDetailPanel;
import frame.PayMain.PayMainPanel;
import frame.yuangong.caiguo;
import frame.yuangong.goods;
import frame.yuangong.yuangonggerxinxi;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.EmployeeMapper;
import org.example.pojo.Employee;
import redis.clients.jedis.Jedis;

public class yuangongPabel extends JPanel {
    private static JDesktopPane jDesktopPane = new JDesktopPane();
    private BufferedImage backgroundImage;

    public SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

    // Redis 连接信息
    private static final String REDIS_HOST = "localhost";
    private static final int REDIS_PORT = 6379;
    public static final Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT);

    public yuangongPabel() {
        this.setBounds(0, 0, Constant.width, Constant.height);
        this.setLayout(new BorderLayout());

        // 加载背景图片
        try {
            InputStream imageStream = getClass().getClassLoader().getResourceAsStream("images/chaoshi1.jpg");
            if (imageStream != null) {
                backgroundImage = ImageIO.read(imageStream);
            } else {
                System.err.println("未找到图片: images/chaoshi1.jpg");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();
        this.add(menuBar, BorderLayout.NORTH);

        // 添加 JDesktopPane 到中心
        ((JComponent) jDesktopPane).setOpaque(false); // 设置 JDesktopPane 透明
        this.add(jDesktopPane, BorderLayout.CENTER);

        // 创建顶级菜单
        JMenu manageMenu = new JMenu("普通用户管理");
        JMenu manageMenu1 = new JMenu("设置");



        // 添加顶级菜单到菜单栏
        menuBar.add(manageMenu);
        menuBar.add(manageMenu1);
        // 添加管理菜单项
        JMenuItem manageEmployees = new JMenuItem("个人信息");
        JMenuItem manageGoods = new JMenuItem("商品查询");
        JMenuItem manageOrders = new JMenuItem("采购信息");

        manageMenu.add(manageEmployees);
        manageMenu.add(manageGoods);
        manageMenu.add(manageOrders);
        manageEmployees.addActionListener(e -> setContent(new yuangonggerxinxi()));
        manageGoods.addActionListener(e -> setContent(new goods()));
        manageOrders.addActionListener(e -> setContent(new caiguo()));
        JMenuItem exitItem = new JMenuItem("退出");
        manageMenu1.add(exitItem);
        exitItem.addActionListener(e -> AppFrame.setContent(new LandPanel()));
        // 监听父容器大小变化事件
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                adjustDesktopPaneSize();
            }
        });

        adjustDesktopPaneSize(); // 初始调整桌面面板大小
    }

    // 设置内容面板的方法
    public static void setContent(JInternalFrame internalFrame) {
        internalFrame.setVisible(true);
        jDesktopPane.removeAll();
        jDesktopPane.add(internalFrame);
        internalFrame.setSize(jDesktopPane.getWidth(), jDesktopPane.getHeight());
        internalFrame.revalidate(); // 重新布局内部组件
        internalFrame.repaint(); // 刷新界面
    }

    // 调整桌面面板大小的方法
    private void adjustDesktopPaneSize() {
        jDesktopPane.setSize(getWidth(), getHeight());
        Component[] components = jDesktopPane.getComponents();
        for (Component component : components) {
            if (component instanceof JInternalFrame) {
                JInternalFrame internalFrame = (JInternalFrame) component;
                internalFrame.setSize(jDesktopPane.getWidth(), jDesktopPane.getHeight());
                internalFrame.revalidate();
                internalFrame.repaint();
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        } else {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }
}
